package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.Message;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.User;
import com.yq.cp.service.basics.MessageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 消息 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-10-12
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageService messageService;

    /**
     * 消息列表   type  1-工单  2-巡检  3-报警
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        User user = UserThreadLocal.get();
        List<Message> list = messageService.list(new QueryWrapper<Message>()
                .eq("user_id", user.getId()).eq("type", vo.getType())
                .orderByDesc("create_time"));
        return  ApiResult.success(list);
    }

    /**
     * 确认消息
     * @param message
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody Message message){
        messageService.saveOrUpdate(message);
        return  ApiResult.success();
    }

}